package com.facade;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import com.common.constant.DateConstant;
import com.common.util.JasperUtil;
import com.dao.MemberDAO;
import com.dao.MembershipPaymentDAO;
import com.model.Member;
import com.model.Membership;
import com.model.MembershipPayment;
import com.objects.MembershipSearch;
import com.objects.PaymentSearch;

public class MembershipPaymentFacade implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private MembershipPaymentDAO membershipPaymentDAO = new MembershipPaymentDAO();
	private MemberDAO memberDAO = new MemberDAO();

	public void createMembershipPayment(MembershipPayment membershipPayment) {
		membershipPaymentDAO.beginTransaction();
		membershipPaymentDAO.save(membershipPayment);
		membershipPaymentDAO.commitAndCloseTransaction();
		
		MemberFacade memberFacade = new MemberFacade();
		memberFacade.extendMembershipExpiry(membershipPayment.getMember());
	}
	
	public List<MembershipPayment> listAll() {
		membershipPaymentDAO.beginTransaction();
		List<MembershipPayment> result = membershipPaymentDAO.findAll();
		membershipPaymentDAO.closeTransaction();
		return result;
	}	
	
	public List<MembershipPayment> listActive() {
		membershipPaymentDAO.beginTransaction();
		List<MembershipPayment> result = membershipPaymentDAO.listActive();
		membershipPaymentDAO.closeTransaction();
		
		return result;
	}		
	
	public List<Member> activeMemberAutoComplete(String name){
		memberDAO.beginTransaction();
		List<Member> resultList = memberDAO.activeMemberAutoComplete(name);
		memberDAO.closeTransaction();
		
		return resultList;
	}
	
	public File generateReceipt(MembershipPayment membershipPayment){
		
		JasperUtil jasperUtil = new JasperUtil();
		String jasperPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/");
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("LOGO_PATH", jasperPath + "/JASPER/" + "maxout_logo.jpg");
		
		ArrayList<MembershipPayment> dataBean = new ArrayList<MembershipPayment>();
		dataBean.add(membershipPayment);
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(dataBean);
		
		File receiptFile = jasperUtil.JasperReportGenerator("reportv2.jrxml", parameters, dataSource);

		return receiptFile;
	}
	
	public List<MembershipPayment> search(PaymentSearch paymentSearch){
		
		Date paymentFrom = paymentSearch.getPaymentFrom();
		if(paymentFrom == null){
			paymentFrom = DateConstant.MIN_DATE;
		}
		
		Date paymentTo = paymentSearch.getPaymentTo();
		if(paymentTo == null){
			paymentTo = DateConstant.MAX_DATE;
		}
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("member", "%"+paymentSearch.getName()+"%");
		parameters.put("membership", "%"+paymentSearch.getMembershipPackage()+"%");
		parameters.put("paymentFrom", paymentFrom);
		parameters.put("paymentTo", paymentTo);
		
		membershipPaymentDAO.beginTransaction();
		List<MembershipPayment> resultList = membershipPaymentDAO.search(parameters);
		membershipPaymentDAO.closeTransaction();
		
		return resultList;
	}	

}
